<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-echart" ref="map_ref"></div>
  </div>
</template>

<script>
import axios from "axios";
import { mapState } from 'vuex';
import { getProvinceMapInfo } from "@/utils/map_utils";
export default {
  data() {
    return {
      echartInstance: null,
      // 服务器返回的数据
      allData: null,
      // 所获取的省份的地图矢量数据
      mapData: {}
    };
  },
  computed:{
    ...mapState(['theme']),
  },
  watch:{
    theme(){
      // 销毁当前图表
      this.echartInstance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    }
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    // 首次适配
    this.screenAdapter();
  },
  beforeDestroy() {
    // 移除resize事件
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    // 初始化echartsInstance对象
    async initChart() {
      this.echartInstance = this.$echarts.init(this.$refs.map_ref, this.theme);
      // 获取中国地图的矢量数据
      const ret = await axios.get(
        "http://localhost:8999/static/map/china.json"
      );
      this.$echarts.registerMap("china", ret.data);
      // 对图表初始化配置的控制
      const initOption = {
        title: {
          text: "▎商家分布",
          left: 20,
          top: 20
        },
        geo: {
          type: "map",
          map: "china",
          top: "5%",
          bottom: "5%",
          itemStyle: {
            // 地图区域的颜色
            areaColor: "pink",
            // 地图板块的线条颜色
            borderColor: "#333"
          }
        },
        legend: {
          left: "5%",
          bottom: "5%",
          // 图例排列方式:垂直
          orient: "vertical"
        }
      };
      this.echartInstance.setOption(initOption);
      // 对图表进行鼠标事件监听
      this.echartInstance.on("click", async arg => {
        console.log("click:", arg);
        // arg.name 省份中文名
        const provinceInfo = getProvinceMapInfo(arg.name);
        // 获取省份地图矢量数据
        if (!this.mapData[provinceInfo.key]) {
          const ret = await axios.get(
            "http://localhost:8999" + provinceInfo.path
          );
          this.mapData[provinceInfo.key] = ret.data;
          this.$echarts.registerMap(provinceInfo.key, ret.data);
        }
        const changeOption = {
          geo: {
            map: provinceInfo.key
          }
        };
        this.echartInstance.setOption(changeOption);
      });
    },
    // 获取服务器数据
    async getData() {
      const { data: ret } = await this.$http.get("map");
      this.allData = ret;
      this.updateChart();
    },
    // 更新图表
    updateChart() {
      // 图例数据
      const legendArr = this.allData.map(v => v.name);
      const seriesArr = this.allData.map(v => {
        // 一个类别下的所有散点数据
        // 如果想在地图中显示散点的数据，需要使用地图坐标系统：coordinateSystem:geo
        return {
          type: "effectScatter",
          rippleEffect: {
            scale: 5,
            // 空心涟漪
            brushType: "stroke"
          },
          name: v.name,
          data: v.children,
          coordinateSystem: "geo"
        };
      });
      const dataOption = {
        legend: {
          data: legendArr
        },
        series: seriesArr
      };
      this.echartInstance.setOption(dataOption);
    },
    // 图表对屏幕的适配
    screenAdapter() {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          },
          legend: {
            itemWidth: titleFontSize / 2,
            itemheight: titleFontSize / 2,
            // 图例的间隔
            itemGap: titleFontSize / 2,
            textStyle: {
              fontSize: titleFontSize / 2
            }
          }
        }
      };
      this.echartInstance.setOption(adapterOption);
      // 手动调用图表对象的resize以适配
      this.echartInstance.resize();
    },
    // 双击会到中国地图
    revertMap() {
      const revertOption = {
        geo: {
          map: "china"
        }
      };
      this.echartInstance.setOption(revertOption);
    }
  }
};
</script>

<style lang="less" scoped></style>
